#include "threadPool.h"
void *threadFunc(void *arg){
    threadPool_t *pthreadPool = (threadPool_t *)arg;
    while(1){
        int netfd;
        pthread_mutex_lock(&pthreadPool->mutex);

        while(pthreadPool->taskQueue.queueSize == 0 && pthreadPool->exitFlag == 0){
            pthread_cond_wait(&pthreadPool->cond,&pthreadPool->mutex);
        }
        if(pthreadPool->exitFlag == 1){
            printf("A worker is going to exit!\n");
            pthread_mutex_unlock(&pthreadPool->mutex);
            pthread_exit(NULL);
        }

        int goal = pthreadPool->taskQueue.pFront->data;
        /* if(goal == 1){ */
        /*     printf("调用操作1\n"); */
        /* } */
        netfd = pthreadPool->taskQueue.pFront->netfd;
        deQueue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->mutex);
        printf("i am niger! i got netfd = %d\n",netfd);
        /* if(goal  == 1){ */
        /*     printf("调用操作1\n"); */
        /* } */
        switch(goal){
        case 1:
            printf("op1\n");
            close(netfd);
            continue;
        case 2:
            printf("op2\n");
            close(netfd);
            continue;
        case 3:
            printf("op3\n");

            transfile(netfd);
            close(netfd);
            continue;
        case 4:
            printf("op4\n");
            /* close(netfd); */
            continue;
        case 5:
            printf("op5\n");
            /* close(netfd); */
            continue;
        case 6:
            printf("op6\n");
            /* close(netfd); */
            continue;
        }
        /* transfile(netfd); */
        /* close(netfd); */
    }
}

int makeWorker(threadPool_t *pthreadPool){
    for(int i = 0; i < pthreadPool->tidArr.workerNum;i++){
        int ret = pthread_create(&pthreadPool->tidArr.arr[i],NULL,threadFunc,pthreadPool);
        ERROR_CHECK(ret,-1,"pthread_create");
        printf("create pthread ok\n");
    }
    return 0;
}
